package com.jztd.servone.invoice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.xbatis.datasource.routing.DS;
import com.jztd.platfrom.base.exception.BusinessException;
import com.jztd.platfrom.base.exception.ResourceNotFoundException;
import com.jztd.servone.invoice.mapper.MerchantsMapper;
import com.jztd.servone.invoice.po.Merchants;
import com.jztd.servone.invoice.service.MerchantsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@DS("master")
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MerchantsServiceImpl implements MerchantsService {
    private final MerchantsMapper merchantsMapper;

    @Override
    public Merchants getMerchants(String merchantNo, String businessType) {
        try {
            var merchants = merchantsMapper.get(where -> {
                where.eq(Merchants::getMerchantNo, merchantNo)
                        .eq(Merchants::getStatus, 0);
            });
            if (ObjectUtil.isEmpty(merchants)) {
               throw new ResourceNotFoundException("商户不存在");
            }
            Merchants merchantsData = merchantsMapper.get(where -> {
                where.eq(Merchants::getAppId, merchants.getAppId())
                        .eq(Merchants::getBusinessType, businessType);
            });
            return merchantsData;
        } catch (Exception e) {
            log.error("查询商户信息异常:{}:{}", e.getMessage(), e);
            throw new BusinessException("查询商户信息异常");
        }
    }
}
